﻿using MediatR;
using Microsoft.AspNetCore.Components.Routing;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.MainPlans;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week2.Mainplan_CRUD
{
    /// <summary>
    /// 添加主生产计划
    /// </summary>
    public class AddMainplanHandler : IRequestHandler<AddMainplanCommand, Result>
    {
        public AddMainplanHandler(IBaseRepository<Mainpalan> mainpalanRepository, IBaseRepository<Mainplandetail> mainplandetailRepository)
        {
            MainpalanRepository = mainpalanRepository;
            MainplandetailRepository = mainplandetailRepository;
        }

        public IBaseRepository<Mainpalan> MainpalanRepository { get; set; }
        public IBaseRepository<Mainplandetail> MainplandetailRepository { get; set; }
        public async Task<Result> Handle(AddMainplanCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddMainplanData = "insert into mainpalans values(@MainplanId,@MainplanNum,@MainplanName,@MainplanContext,@MainplanState,@CreateName,@CreateSection,@CreateTime,@ApprovalState,@IsDelMainplan,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
            var AddNowNum = MainpalanRepository.Add(AddMainplanData, new
            {
                MainplanId = request.MainplanId,
                MainplanNum = request.MainplanNum,
                MainplanName = request.MainplanName,
                MainplanContext = request.MainplanContext,
                MainplanState = request.MainplanState,
                CreateName = request.CreateName,
                CreateSection = request.CreateSection,
                CreateTime = request.CreateTime,
                ApprovalState = request.ApprovalState,
                IsDelMainplan = request.IsDelMainplan,
                Handle = request.Handle,
                HandleContext = request.HandleContext,
                HandleTime = request.HandleTime,
                Pid = request.Pid,
                IsDelete = request.IsDelete,
            });
            if (AddNowNum > 0)
            {
                var AddDetailSql = "insert into mainplandetails values(@MainplandetailId,@MainplanNum,@SuppliesData,@SuppliesNature,@Unit,@Specification,@BomVersions,@MainplandetailNum,@CreateTime,@FinishTime,@Remark,@IsDelMainplandetail,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete,@ItemCode);";
                int AddDetailNum = 0;
                foreach (var item in request.mainplandetails)
                {
                    AddDetailNum += MainplandetailRepository.Add(AddDetailSql, new
                    {
                        MainplandetailId = 0,
                        MainplanNum = item.MainplanNum,
                        ItemCode=item.ItemCode,
                        SuppliesData = item.SuppliesData,
                        SuppliesNature = item.SuppliesNature,
                        Unit = item.Unit,
                        Specification = item.Specification,
                        BomVersions = item.BomVersions,
                        MainplandetailNum = item.MainplandetailNum,
                        CreateTime = item.CreateTime,
                        FinishTime = item.FinishTime,
                        Remark = item.Remark,
                        IsDelMainplandetail = item.IsDelMainplandetail,
                        Handle = request.Handle,
                        HandleContext = request.HandleContext,
                        HandleTime = request.HandleTime,
                        Pid = request.Pid,
                        IsDelete = request.IsDelete,
                    });
                }
                if (AddDetailNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    return result;
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
    }
}
